package com.woldier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woldier.common.MyException;
import com.woldier.common.R;
import com.woldier.dto.SetmealDto;
import com.woldier.entity.Setmeal;
import com.woldier.service.SetmealService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 新增套餐
     * @param setmeal
     * @return
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping
    public R insert(@RequestBody SetmealDto setmeal){
        log.info(setmeal.toString());

        setmealService.saveWithDish(setmeal);
        return R.success("成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("page")
    public R getPage(int page, int pageSize, String name){

        /*分页构造*/
        Page<Setmeal> page1 = new Page<>();
        /*条件构造构造器*/
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper  = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(name!=null,Setmeal::getName,name);

        setmealService.page(page1,lambdaQueryWrapper);

        return R.success(page1);
    }

    /**
     * 更新状态
     * @param ids
     * @param status
     * @return
     */
    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping("status/{status}")
    public R updateStatus(Long[] ids,@PathVariable(value = "status",required = false) Integer status){
        /*封装setmeal以及status*/
        List<Setmeal> list = getSetmealList(ids,status);
        setmealService.updateBatchById(list);
        return R.success("成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     * @throws MyException
     */

    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) throws MyException {
        setmealService.removeBatchWithDish(ids);
        return R.success("成功");
    }


    /**
     * 用户界面套餐展示
     * @param setmeal
     * @return
     */

    @Cacheable(value = "setmeal" , key = "#setmeal.categoryId+'_'+#setmeal.status" )
    @GetMapping("list")
    public R getSetmeal( Setmeal setmeal){
        /*构造查询*/
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId()).eq(Setmeal::getStatus,setmeal.getStatus());
//        select * from setmeal where category_id = ? and status = 1
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);

        return R.success(list);

    }




    /*-------------------------------------------------------------------*/

    private List<Setmeal> getSetmealList(Long[] ids,Integer status){
        List<Setmeal> list = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            list.add(setmeal);
        }

        return list;
    }


}


